package cn.jbolt.admin.post;

import cn.jbolt.core.api.JBoltApiBaseController;
import cn.jbolt.core.api.httpmethod.JBoltHttpGet;
import cn.jbolt.core.api.httpmethod.JBoltHttpPost;
import cn.jbolt.core.model.Post;
import cn.jbolt.core.model.base.JBoltBaseModel;
import cn.jbolt.core.permission.CheckPermission;
import cn.jbolt.core.permission.UnCheck;
import cn.jbolt.core.permission.UnCheckIfSystemAdmin;
import cn.jbolt.core.service.JBoltPostService;
import cn.jbolt.core.service.base.JBoltBaseService;
import cn.jbolt.extend.permission.PermissionKey;
import com.jfinal.aop.Before;
import com.jfinal.aop.Inject;
import com.jfinal.core.Path;
import com.jfinal.core.paragetter.Para;
import com.jfinal.plugin.activerecord.tx.Tx;

@UnCheckIfSystemAdmin
@CheckPermission(PermissionKey.POST)
@Path("/api/admin/post")
public class PostAdminController extends JBoltApiBaseController {

    @Inject
    private JBoltPostService service;

    @Override
    protected JBoltBaseService<? extends JBoltBaseModel<?>> getModelService() {
        return service;
    }

    /**
     * 单个详情
     */
    @JBoltHttpGet
    public void index() {
        renderJBoltApiRet(findModelById());
    }

    /**
     * 分页条件查询数据源
     * @param page
     * @param pageSize
     * @param type
     * @param enable
     * @param keywords
     */
    @JBoltHttpGet
    public void datas(@Para(value = "page",defaultValue = "1")Integer page,
                      @Para(value = "pageSize",defaultValue = "15")Integer pageSize,
                      @Para("type")Integer type,
                      @Para("enable")Boolean enable,
                      @Para("keywords")String keywords) {
            renderJBoltApiSuccessWithData(service.paginateAdminDatas(page,pageSize,type,enable,keywords));
    }

    /**
     * select数据源options
     */
    @UnCheck
    @JBoltHttpGet
    public void options() {
        renderJBoltApiSuccessWithData(service.getEnableOptionsList());
    }



    /**
     * 保存
     */
    @Before(Tx.class)
    @JBoltHttpPost
    public void save(@Para("") Post post) {
        renderJBoltApiRet(service.save(post));
    }

    /**
     * 更新
     */
    @Before(Tx.class)
    @JBoltHttpPost
    public void update(@Para("")Post post) {
        renderJBoltApiRet(service.update(post));
    }

    /**
     * 删除
     */
    @Before(Tx.class)
    @JBoltHttpGet
    public void delete() {
        renderJBoltApiRet(deleteModelById());
    }

    /**
     * 切换启用状态
     */
    @Before(Tx.class)
    @JBoltHttpGet
    public void toggleEnable() {
        renderJBoltApiRet(toggleModelEnableById());
    }

    /**
     * 排序 上移
     */
    @Before(Tx.class)
    @JBoltHttpGet
    public void up() {
        renderJBoltApiRet(service.up(getId()));
    }

    /**
     * 排序 下移
     */
    @Before(Tx.class)
    @JBoltHttpGet
    public void down() {
        renderJBoltApiRet(service.down(getId()));
    }

    /**
     * 排序 初始化
     */
    @Before(Tx.class)
    @JBoltHttpGet
    public void initRank() {
        renderJBoltApiRet(service.initRank());
    }

}
